/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.game.wordpuzzle;

import java.util.HashMap;
import java.util.Iterator;

/**
 *
 * @author AritomoAbe
 */
public class GameManager {

    private static int index = 0;
    private static String[][] gameVars = new String[][]{
        {"Question 1?", "Answer1"},
        {"Question 2?", "Answer2"},};
    private static GameManager gameManager;
    private int gameCount = 0;
    private static HashMap<String, Game> activeGames = new HashMap<String, Game>();

    public static GameManager getGameManager() {
        if (gameManager == null) {
            gameManager = new GameManager();
        }
        return gameManager;
    }

    public String listActiveGames() {
        StringBuffer sResult = new StringBuffer();
        Iterator<String> iter = activeGames.keySet().iterator();
        while (iter.hasNext()) {
            sResult.append(iter.next());
            sResult.append(";");
        }
        return sResult.toString();
    }

    public Game getActiveGame(String gameId) {
        return activeGames.get(gameId);
    }

    public String startGame() {
        Game game = new Game();
        if (index >= gameVars.length) {
            index = 0;
        } else {
            index++;
        }

        game.setId(gameCount++);
        game.setQuestion(gameVars[index][0]);
        game.setAnswer(gameVars[index][1]);
        activeGames.put(String.valueOf(game.getId()), game);
        return String.valueOf(game.getId());
    }

    public void stopGame(String gameId) {
        activeGames.remove(gameId);
    }
}
